<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {
        }
        if ($this->auth->id != 1) {
            $where['admin_id'] = $this->auth->id;
        } else {
            $where = [];
        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        // $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
        //     ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
        //     ->group('join_date')
        //     ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $date = date("m-d", $time);
            $column[] = $date;
            $t_s = $time;
            $time += 86400;
            $t_end = $time;
            $num =  db('user')->whereBetween('jointime', [$t_s, $t_end])->where($where)->count('id');
            $userlist[$date] = $num;
            $num =  db('kd_order')->whereBetween('createtime', [$t_s, $t_end])->where($where)->count('id');
            $kdorderNum[$date] = $num;

            $num =  db('hf_order')->whereBetween('createtime', [$t_s, $t_end])->where($where)->count('id');
            $hforderNum[$date] = $num;

            $num =  db('kd_order')->whereBetween('createtime', [$t_s, $t_end])->where($where)->sum('price');
            $kdMoney[$date] = round($num, 2);

            $num =  db('hf_order')->whereBetween('createtime', [$t_s, $t_end])->where($where)->sum('price');
            $hfMoney[$date] = round($num, 2);
        }




        $admin = db::table('fa_admin')->where('id', $this->auth->id)->find();
        $totaluser = db::table('fa_user')->where($where)->count('id');

        $orderNum = db::table('fa_hf_order')->where($where)->count('id');



        $moneyALL = db::table('fa_hf_order')->where($where)->sum('price');

        $todayusersignup = db('hf_order')->whereTime('createtime', 'today')->where($where)->count('id');
        $todayuserlogin =  db('hf_order')->whereTime('createtime', 'today')->where($where)->sum('price');
        $threednu =  db('hf_order')->whereTime('createtime', 'yesterday')->where($where)->count('id');
        // return  $threednu;
        $sevendnu =  db('hf_order')->whereTime('createtime', 'yesterday')->where($where)->sum('price');

        $sevendau = db('hf_order')->whereTime('createtime', '-7 days')->where($where)->count('id');
        $thirtydau =  db('hf_order')->whereTime('createtime', '-7 days')->where($where)->sum('price');
        $num1 = db('hf_order')->whereTime('createtime', 'month')->where($where)->count('id');
        $num2 = db('hf_order')->whereTime('createtime', 'month')->where($where)->sum('price');
        $num3 = db('hf_order')->whereTime('createtime', 'last month')->where($where)->count('id');
        $num4 = db('hf_order')->whereTime('createtime', 'last month')->where($where)->sum('price');

        $num5 =  db('user')->whereTime('jointime', 'month')->where($where)->count('id');
        $num6 =  db('user')->whereTime('jointime', 'last month')->where($where)->count('id');

        $num8 = db('hf_order')->where('cztype', '1')->where($where)->count('id');
        $num7 = db('hf_order')->where('cztype', '2')->where($where)->count('id');
        $money = $admin['money'];
        $this->view->assign([
            'totaluser'         => $totaluser,
            'totaladdon'        => $orderNum,
            'attachmentnums'    => round($moneyALL, 2),
            'totaladmin'        => $money,
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => $todayusersignup,
            'todayuserlogin'    => round($todayuserlogin, 2),
            'sevendau'          => $sevendau,
            'sevendnu'         => round($sevendnu, 2),
            'threednu'          => $threednu,
            'thirtydau'          => round($thirtydau, 2),
            'num1'       => $num1,
            'num2'            => round($num2, 2),
            'num3' => $num3,
            'num4'    => round($num4, 2),
            'num5' => $num5,
            'num6'    => $num6,
            'num8'    => $num8,
            'num7'    => $num7,
        ]);
        //return json_encode($userlist);
        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));
        $this->assignconfig('kdorderNum', array_values($kdorderNum));
        $this->assignconfig('hforderNum', array_values($hforderNum));
        $this->assignconfig('kdMoney', array_values($kdMoney));
        $this->assignconfig('hfMoney', array_values($hfMoney));
        return $this->view->fetch();
    }
}
